package plugins

import (
	"data-transfer/conf"
	"fmt"
)

var supportedPlugins = map[string]Plugin{}

// RegisterPlugin 注册一个数据源plugin
func RegisterPlugin(pluginName string, plugin Plugin) {
	if _, ok := supportedPlugins[pluginName]; ok {
		panic("plugin " + pluginName + " already registered.")
	}
	supportedPlugins[pluginName] = plugin
}

//NewSQLDB 返回底层数据库【逻辑连接】，返回的对象被绑定到单个数据库，并且可以对数据库表进行CRUD操作
func NewSQLDB(dbKind DbKind, cfg *conf.DbConfig) (DB, error) {
	plugin, ok := supportedPlugins[cfg.PluginName]
	if !ok {
		return nil, fmt.Errorf("not supported plugin %v,only supported: %v", cfg.PluginName, supportedPlugins)
	}

	return plugin.CreateDB(dbKind, cfg)
}
